// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringEnum.cs" company="GT">
//   Gunther Taborga
// </copyright>
// <summary>
//   Defines the StringEnum type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace BatchID3.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using BatchID3.Attributes;

    public static class StringEnum
    {
        public static string GetFirstStringValue(this Enum value)
        {
            var attributes = GetStringValueAttributes(value);
            var result = string.Empty;

            if (attributes != null && attributes.Length > 0)
            {
                result = attributes[0].Value;
            }

            return result;
        }

        public static IEnumerable<string> GetAllStringValues(this Enum value)
        {
            var attributes = GetStringValueAttributes(value);
            var result = new List<string>();

            if (attributes != null)
            {
                result.AddRange(attributes.Select(attribute => attribute.Value));
            }

            return result;
        }

        private static StringValue[] GetStringValueAttributes(Enum value)
        {
            var type = value.GetType();
            var fieldInfo = type.GetField(value.ToString());
            var attributes = fieldInfo.GetCustomAttributes(typeof(StringValue), false) as StringValue[];

            return attributes;
        }
    }
}